/**
 * Copyright (c) 2005-2012 springside.org.cn
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package reminder.api.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.Map.Entry;

/**
 * description:
 * saras_xu@163.com 2017-11-29 14:27 创建
 */
public class ServletUtils {

    // -- 常用数值定义 --//
    public static final long ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;

    public static void writeResponse(HttpServletResponse response, String data) {
        OutputStream output = null;
        InputStream input = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType(MediaType.JSON_UTF_8.toString());
            output = response.getOutputStream();
            input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
            IOUtils.copy(input, output);
            output.flush();
        } catch (Exception e) {
            throw new RuntimeException("响应请求(flushResponse)失败:"
                    + e.getMessage());
        } finally {
            IOUtils.closeQuietly(output);
            IOUtils.closeQuietly(input);
        }
    }

    /**
     * 设置客户端缓存过期时间 的Header.
     */
    public static void setExpiresHeader(HttpServletResponse response,
                                        long expiresSeconds) {
        // Http 1.0 header, set a fix expires date.
        response.setDateHeader(HttpHeaders.EXPIRES, System.currentTimeMillis()
                + expiresSeconds * 1000);
        // Http 1.1 header, set a time after now.
        response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age="
                + expiresSeconds);
    }

    /**
     * 设置禁止客户端缓存的Header.
     */
    public static void setNoCacheHeader(HttpServletResponse response) {
        // Http 1.0 header
        response.setDateHeader(HttpHeaders.EXPIRES, 1L);
        response.addHeader(HttpHeaders.PRAGMA, "no-cache");
        // Http 1.1 header
        response.setHeader(HttpHeaders.CACHE_CONTROL,
                "no-cache, no-store, max-age=0");
    }

    /**
     * 设置LastModified Header.
     */
    public static void setLastModifiedHeader(HttpServletResponse response,
                                             long lastModifiedDate) {
        response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModifiedDate);
    }

    /**
     * 设置Etag Header.
     */
    public static void setEtag(HttpServletResponse response, String etag) {
        response.setHeader(HttpHeaders.ETAG, etag);
    }

    /**
     * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
     * <p>
     * 如果无修改, checkIfModify返回false ,设置304 not modify status.
     *
     * @param lastModified 内容的最后修改时间.
     */
    public static boolean checkIfModifiedSince(HttpServletRequest request,
                                               HttpServletResponse response, long lastModified) {
        long ifModifiedSince = request
                .getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
        if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return false;
        }
        return true;
    }

    /**
     * 根据浏览器 If-None-Match Header, 计算Etag是否已无效.
     * <p>
     * 如果Etag有效, checkIfNoneMatch返回false, 设置304 not modify status.
     *
     * @param etag 内容的ETag.
     */
    public static boolean checkIfNoneMatchEtag(HttpServletRequest request,
                                               HttpServletResponse response, String etag) {
        String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
        if (headerValue != null) {
            boolean conditionSatisfied = false;
            if (!"*".equals(headerValue)) {
                StringTokenizer commaTokenizer = new StringTokenizer(
                        headerValue, ",");

                while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                    String currentToken = commaTokenizer.nextToken();
                    if (currentToken.trim().equals(etag)) {
                        conditionSatisfied = true;
                    }
                }
            } else {
                conditionSatisfied = true;
            }

            if (conditionSatisfied) {
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader(HttpHeaders.ETAG, etag);
                return false;
            }
        }
        return true;
    }

    /**
     * 设置让浏览器弹出下载对话框的Header.
     *
     * @param fileName 下载后的文件名.
     */
    public static void setFileDownloadHeader(HttpServletResponse response,
                                             String fileName) {
        try {
            // 中文文件名支持
            String encodedfileName = new String(fileName.getBytes("UTF-8"),
                    "ISO8859-1");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                    "attachment; filename=\"" + encodedfileName + "\"");
        } catch (UnsupportedEncodingException e) {
        }
    }

    /**
     * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
     * <p>
     * 返回的结果的Parameter名已去除前缀.
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, Object> getParametersStartingWith(
            ServletRequest request, String prefix) {
        Validate.notNull(request, "Request must not be null");
        Enumeration paramNames = request.getParameterNames();
        Map<String, Object> params = new TreeMap<String, Object>();
        if (prefix == null) {
            prefix = "";
        }
        while (paramNames != null && paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            if ("".equals(prefix) || paramName.startsWith(prefix)) {
                String unprefixed = paramName.substring(prefix.length());
                String[] values = request.getParameterValues(paramName);
                if (values.length > 1) {
                    params.put(unprefixed, values);
                } else if (values.length == 1) {
                    params.put(unprefixed, values[0]);
                }
            }
        }
        return params;
    }

    /**
     * 组合Parameters生成Query String的Parameter部分, 并在paramter name上加上prefix.
     *
     * @see #getParametersStartingWith
     */
    public static String encodeParameterStringWithPrefix(
            Map<String, Object> params, String prefix) {
        if (params == null || params.size() == 0) {
            return "";
        }

        if (prefix == null) {
            prefix = "";
        }

        StringBuilder queryStringBuilder = new StringBuilder();
        Iterator<Entry<String, Object>> it = params.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Object> entry = it.next();
            queryStringBuilder.append(prefix).append(entry.getKey())
                    .append('=').append(entry.getValue());
            if (it.hasNext()) {
                queryStringBuilder.append('&');
            }
        }
        return queryStringBuilder.toString();
    }

    /**
     * 获取请求的URL路径
     *
     * @param request
     * @return
     */
    public static String getRequestPath(HttpServletRequest request) {
        return StringUtils.substringAfter(request.getRequestURI(),
                request.getContextPath());
    }

    public static String getRequestPage(HttpServletRequest request) {
        return StringUtils.substringAfterLast(request.getRequestURI(), "/");
    }

    public static String getRequestFullPath(HttpServletRequest request) {
        return getRequestPath(request) + "?" + request.getQueryString();
    }

    public static String getHeaderValue(HttpServletRequest request, String name) {
        String value = request.getHeader(name);
        if (StringUtils.isBlank(value)) {
            value = request.getHeader(StringUtils.lowerCase(name));
        }
        return value;
    }

    @SuppressWarnings("unchecked")
    public static List<String> getHeaderValues(HttpServletRequest request,
                                               String name) {
        Enumeration<String> e = request.getHeaders(name);
        List<String> values = Lists.newArrayList();
        while (e.hasMoreElements()) {
            values.add(e.nextElement());
        }
        return values;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, String> getHeaders(HttpServletRequest request,
                                                 String prefixName) {
        Enumeration<String> names = request.getHeaderNames();
        String name = null;
        Map<String, String> map = Maps.newLinkedHashMap();
        while (names.hasMoreElements()) {
            name = names.nextElement();
            if (StringUtils.isNotBlank(prefixName)) {
                if (StringUtils.startsWithIgnoreCase(name, prefixName)) {
                    map.put(StringUtils.lowerCase(name),
                            getHeaderValue(request, name));
                }
            } else {
                map.put(StringUtils.lowerCase(name),
                        getHeaderValue(request, name));
            }
        }
        return map;
    }

    public static void setHeader(HttpServletResponse response, String name,
                                 String value) {
        response.setHeader(name, value);
    }

    public static void setHeaders(HttpServletResponse response,
                                  Map<String, String> map) {
        for (Entry<String, String> entry : map.entrySet()) {
            setHeader(response, entry.getKey(), entry.getValue());
        }
    }

    public static String getQueryString(HttpServletRequest request) {
        return getRequestPath(request) + "?" + request.getQueryString();
    }

}
